package com.yunzhong.appointment.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.yunzhong.appointment.entity.SysMenu;
import com.yunzhong.appointment.entity.SysRole;
import com.yunzhong.appointment.entity.SysRoleMenu;
import com.yunzhong.appointment.mapper.SysMenuMapper;
import com.yunzhong.appointment.mapper.SysRoleMapper;
import com.yunzhong.appointment.mapper.SysRoleMenuMapper;
import com.yunzhong.appointment.mapper.SysUserRoleMapper;
import com.yunzhong.appointment.system.service.SysRoleService;
import com.yunzhong.appointment.util.PageData;
import com.yunzhong.appointment.util.SelectOptions;
/**
 * 
 * @className SysRoleServiceImpl
 * @description 角色模型层实现类
 * @author 石洪刚
 * @time 2017年11月13日 下午2:53:08
 */
@Service
public class SysRoleServiceImpl implements SysRoleService{

	@Autowired
	private SysRoleMapper roleMapper;
	@Autowired
	private SysRoleMenuMapper roleMenuMapper;
	@Autowired
	private SysUserRoleMapper userRoleMapper;
	@Autowired
	private SysMenuMapper menuMapper;
	
	@Override
	public List<SysRole> listRole(PageData pd) {
		pd.setPageData(pd);
		return roleMapper.listRole(pd);
	}

	@Override
	public SysRole getRoleById(String id) {
		return roleMapper.selectByPrimaryKey(id);
	}

	@Override
	public void saveOrUpdateRole(SysRole role) {
		if("".equals(role.getRoleId())){
			role.setRoleId(UUID.randomUUID().toString());
			role.setRoleState("A");
			roleMapper.insertSelective(role);
		}else{
			roleMapper.updateByPrimaryKeySelective(role);
		}
	}

	@Override
	public void removeRole(String[] ids) {
		for (String id : ids) {
			//删除角色菜单表中对应数据
			roleMenuMapper.removeByRoleId(id);
			//删除用户角色表中对应数据
			userRoleMapper.removeByRoleId(id);
			//删除角色
			roleMapper.deleteByPrimaryKey(id);
		}
	}

	@Override
	public JSONObject checkRoleName(String roleName, String roleId) {
		JSONObject obj = new JSONObject();
		obj.put("result", true);
		String roleIdInDb = roleMapper.getRoleIdByName(roleName);
		if(StringUtils.isNotBlank(roleIdInDb) && !roleIdInDb.equals(roleId)){
			obj.put("result", false);
		}
		return obj;
	}
	
	@Override
	public List<SelectOptions> listRoleMenuByRoleId(String id) {
		List<SelectOptions> topMenuList = menuMapper.listTopMenuToOps();
		//根据角色主键得到所有关联的菜单主键
		List<String> menuIdList = roleMenuMapper.listMenuIdByRoleId(id);
		for (SelectOptions opts : topMenuList) {
			if(menuIdList.contains(opts.getValue())){
				opts.setSelected(true);
			}
			opts = this.recursiveMenuByTopMenu(opts,menuIdList);
		}
		return topMenuList;
	}
	/**
	 * 
	 * @methodName recursiveMenuByTopMenu
	 * @description 递归得到所有菜单
	 * @author 石洪刚
	 * @time 2017年11月14日 下午4:18:35
	 * @param opts
	 * @param menuIdList
	 * @return
	 */
	private SelectOptions recursiveMenuByTopMenu(SelectOptions opts,List menuIdList) {
		List<SelectOptions> childMenus = menuMapper.listMenuByParToOpts(opts);
		for (SelectOptions opt : childMenus) {
			if(menuIdList.contains(opt.getValue())){
				opts.setSelected(true);
				opt.setSelected(true);
			}
			SelectOptions temOpt = recursiveMenuByTopMenu(opt,menuIdList);
			opts.getChildrenList().add(temOpt);
		}
		return opts;
	}

	@Override
	public void saveOrUpdateRoleMenu(String menuIds, String roleId) {
		roleMenuMapper.removeByRoleId(roleId);
		if(!"".equals(menuIds)){
			String[] ids = menuIds.split("@");
			for (int i = 0; i < ids.length; i++) {
				SysRoleMenu srm = new SysRoleMenu();
				srm.setRmId(UUID.randomUUID().toString());
				srm.setMenuId(ids[i]);
				srm.setRoleId(roleId);
				roleMenuMapper.insertSelective(srm);
			}
		}
	}
}
